package com.ybf.user.dal.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ybf.user.dal.entity.MenuDO;
import com.ybf.user.dal.entity.query.MenuQueryDO;
import com.ybf.user.dal.mapper.MenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Component
public class MenuManager extends ServiceImpl<MenuMapper,  MenuDO> {


    @Autowired
    private MenuMapper menuMapper;

    /**
     * 查询
     *
     * @author 向南
     * @date 17:15 2023/8/9
     * @param: mark
     **/
    public MenuDO selectByMark(String mark) {
        if (StringUtils.isEmpty(mark)) {
            return null;
        }
        return menuMapper.selectOne(new LambdaQueryWrapper<MenuDO>().eq(MenuDO::getMark, mark));
    }



    /**
     * 查询列表
     *
     * @author 向南
     * @date 14:14 2023/8/11
     * @param: parentIds
     **/
    public List<MenuDO> selectInParentIds(List<Long> parentIds) {
        if (CollectionUtils.isEmpty(parentIds)) {
            return Collections.emptyList();
        }
        return menuMapper.selectList(new LambdaQueryWrapper<MenuDO>().in(MenuDO::getParentId, parentIds));
    }

    /**
     * 查询
     *
     * @author 向南
     * @date 14:14 2023/8/11
     * @param: menuQueryDO
     **/
    public List<MenuDO> select(MenuQueryDO queryDO) {

        return menuMapper.selectList(new LambdaQueryWrapper<MenuDO>()
                .like(!StringUtils.isEmpty(queryDO.getName()), MenuDO::getName, queryDO.getName())
                .eq(Objects.nonNull(queryDO.getParentId()), MenuDO::getParentId, queryDO.getParentId())
                .isNull(Objects.isNull(queryDO.getParentId()), MenuDO::getParentId).orderByAsc(MenuDO::getSort));
    }

    /**
     * 查询列表
     *
     * @author 向南
     * @date 14:20 2023/8/11
     * @param: maxLevel
     **/
    public List<MenuDO> selectByMaxLevel(Integer maxLevel) {
        if (Objects.isNull(maxLevel)) {
            return Collections.emptyList();
        }
        return menuMapper.selectList(new LambdaQueryWrapper<MenuDO>().le(Objects.nonNull(maxLevel), MenuDO::getLevel, maxLevel));
    }
}
